
import  OSS from 'ali-oss'


import { fetchAliOssSTS } from '@/api/upload';

export default function AliOssClient(bucket = 'rswl-resource') {
  return new Promise((resolve, reject) => {
    fetchAliOssSTS()
      .then((ret) => {
        //console.log('sts ret',JSON.stringify(ret))
        let oss = new OSS({
          region: 'oss-cn-shanghai',
          accessKeyId: ret.accessKeyId,
          accessKeySecret: ret.accessKeySecret,
          bucket: bucket,
          stsToken: ret.securityToken,
          refreshSTSToken: async () => {
            fetchAliOssSTS().then((res) => {
              return {
                accessKeyId: res.accessKeyId,
                accessKeySecret: res.accessKeySecret,
                stsToken: res.securityToken,
              };
            });
          },
          refreshSTSTokenInterval: 300000, //毫秒
        });
        resolve(oss);
      })
      .catch((e) => {
        console.log('e ' + JSON.stringify(e));
        reject(e);
      });
  });
}
